package dao;

import beans.NotaFiscal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class NotaFiscalDAO {

	PreparedStatement pst;
	ResultSet rs;

	public NotaFiscalDAO() {
	}

	public NotaFiscal load(int numero) {
		NotaFiscal nf = new NotaFiscal();

		String query = "SELECT * FROM notafiscal WHERE numero = ?";

		try {
			pst = Conexao.getConexao().prepareStatement(query);
			pst.setInt(1, numero);

			rs = pst.executeQuery();

			if (rs.next()) {
				nf.setNumero(rs.getInt("numero"));
				nf.setValor(rs.getFloat("valor"));
				nf.setDataEmissao(rs.getDate("data"));
				nf.setNumTerceiroReceita(rs.getInt("numterceiroreceita"));
				nf.setNomeTerceiro(rs.getString("nometerceiro"));
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pst != null) {
					pst.close();
				}
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
		}

		return nf;
	}

	public void insert(NotaFiscal nf) {
		String query = "INSERT INTO notafiscal (numero, valor, data_emissao, numterceiroreceita, nometerceiro)"
				+ " VALUES(?, ?, ?, ?, ?)";

		try {
			pst = Conexao.getConexao().prepareStatement(query);

			pst.setInt(1, nf.getNumero());
			pst.setFloat(2, nf.getValor());
			pst.setDate(3, nf.getDataEmissao());
			pst.setInt(4, nf.getNumTerceiroReceita());
			pst.setString(5, nf.getNomeTerceiro());

			pst.executeUpdate();

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pst != null) {
					pst.close();
				}
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
		}
	}

	private void update(NotaFiscal nf) {
		String query = "UPDATE notafiscal"
				+ "SET valor = ?, dataEmissao = ?, numterceiroreceita = ?, nometerceiro = ? "
				+ "WHERE numero = ?";

		try {
			pst = Conexao.getConexao().prepareStatement(query);

			pst.setFloat(1, nf.getValor());
			pst.setDate(2, nf.getDataEmissao());
			pst.setInt(3, nf.getNumTerceiroReceita());
			pst.setString(4, nf.getNomeTerceiro());
			pst.setInt(5, nf.getNumero());

			pst.executeUpdate();

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pst != null) {
					pst.close();
				}
			} catch (SQLException ex) {
				ex.getLocalizedMessage();
			}
		}


	}

	public void save(NotaFiscal nf) {
		String query = "SELECT COUNT(*) FROM notafiscal WHERE numero = ?";

		try {
			pst = Conexao.getConexao().prepareStatement(query);
			pst.setInt(1, nf.getNumero());

			rs = pst.executeQuery();
			rs.next();

			if (rs.getInt("count") != 0) {
				update(nf);
			} else {
				insert(nf);
			}

		} catch (SQLException ex) {
			ex.printStackTrace();

		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pst != null) {
					pst.close();
				}
			} catch (SQLException ex) {
				ex.getLocalizedMessage();
			}
		}
	}

	public void delete(NotaFiscal nf) {
		String query = "DELETE FROM notafiscal WHERE numero = ?";

		try {
			pst = Conexao.getConexao().prepareStatement(query);
			pst.setInt(1, nf.getNumero());

			pst.executeUpdate();

		} catch (SQLException ex) {
			ex.printStackTrace();

		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pst != null) {
					pst.close();
				}
			} catch (SQLException ex) {
				ex.getLocalizedMessage();
			}
		}
	}

	public ArrayList<NotaFiscal> loadAll() {
		ArrayList<NotaFiscal> notas = new ArrayList<NotaFiscal>();

		String query = "SELECT * FROM notafiscal";

		try {
			pst = Conexao.getConexao().prepareStatement(query);
			rs = pst.executeQuery();

			while (rs.next()) {
				NotaFiscal nf = new NotaFiscal();

				nf.setNumero(rs.getInt("numero"));
				nf.setValor(rs.getFloat("valor"));
				nf.setDataEmissao(rs.getDate("data_emissao"));
				nf.setNumTerceiroReceita(rs.getInt("numterceiroreceita"));
				nf.setNomeTerceiro(rs.getString("nomereceita"));

				notas.add(nf);
			}
		} catch (SQLException ex) {
			ex.getLocalizedMessage();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pst != null) {
					pst.close();
				}
			} catch (SQLException ex) {
				ex.getLocalizedMessage();
			}
		}

		return notas;
	}
}
